package devicedbgui;
import devicedbgui.data.ExeDAO;
import devicedbgui.data.TestCaseStats;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/** Displays statistics of test cases */
public class TestCaseStatsPanel extends javax.swing.JPanel {
    
    /** Reference to GUI class for accessing its variables */
    GUI gui;
    String exeDescription;
    /** Constructor creates all components belonging to panel */   
    public TestCaseStatsPanel(GUI gui, String exeDescription) {
        initComponents();
        this.gui = gui;
        this.exeDescription = exeDescription;
        loadData(gui, exeDescription);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblTestCaseStats = new javax.swing.JTable();

        tblTestCaseStats.setAutoCreateRowSorter(true);
        tblTestCaseStats.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Test Case", "Number of times Test Case Failed", "Number of times Test Case Passed"
            }
        ));
        tblTestCaseStats.setGridColor(new java.awt.Color(204, 204, 255));
        tblTestCaseStats.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblTestCaseStats);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
/** Displays error messages */  
    public void showError(JFrame parent, String error)
    {
        JOptionPane.showMessageDialog(parent, error);
    }
/** Displays test case stats from database */
    private void loadData(GUI gui, String exeDescription)
    {
        ExeDAO dd = new ExeDAO(gui.connection);
        try {
            ArrayList<TestCaseStats> records = dd.getAllTestCaseDescriptions(exeDescription);
            DefaultTableModel model = (DefaultTableModel)tblTestCaseStats.getModel();
            model.getDataVector().clear();
                       
            for (TestCaseStats item : records){ 
                model.addRow(new Object[]{item.testCase, item.numberOfFailed, item.numberOfPassed});
            }
        }
        catch (Exception ex){
            showError(gui.frame, ex.toString());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTestCaseStats;
    // End of variables declaration//GEN-END:variables

}
